package;
import fl.data.DataProvider;
import flash.net.NetConnection;
using Chat;
class Chat extends flash.display.MovieClip
{
	var userName : String;
	var output : fl.controls.TextArea;
	var users : fl.controls.List;
	var input : fl.controls.TextInput;
	var btn : fl.controls.Button;
	var connection:NetConnection;
	public function new(connection, userName)
	{
		super();
		this.connection = connection;
		this.userName = userName;
		createView();
	}
	//------- calls TO the server: ------------------------------------
	function onSendBtn(event:flash.events.MouseEvent):Void
	{
		connection.call("setTextInput", null, input.text);
		input.text="";
	}
	//------- called FROM the server: ------------------------------------
	public function onTextInput(user, txt:String):Void
	{
		output.text += formatDate() + " <" + user.name+">\t\t:"+txt+"\n";
	}
	public function onResultGetPlayers(data)
	{
		users.dataProvider = new DataProvider(data);
		users.labelField="name";
	}
	//------- called FROM Lobby: ------------------------------------
	public function onPlayerLeave(user)
	{
		output.text += formatDate() + " <" + user.name+"> left.\n";
	}
	public function onPlayerJoin(user)
	{
		output.text += formatDate()+ " <" + user.name+"> joined.\n";
	}
	function onUserChange(event)
	{
		//log("user info: id:"+users.selectedItem.id+", name:"+users.selectedItem.name);
	}
	function createView()
	{
		var textFormat = new flash.text.TextFormat();
		textFormat.align = flash.text.TextFormatAlign.CENTER;
		
		/*var background = new flash.display.Shape();
		background.graphics.beginFill(0xDDDDFF, 1.0);
		background.graphics.drawRoundRect(0,0,1020, 660, 20, 20);
		addChild(background);
		*/
		users = new fl.controls.List();
		users.x = 10;
		users.y = 10;
		users.width = 200;
		users.height = 500;
		users.addEventListener(flash.events.Event.CHANGE, onUserChange);
		addChild(users);
			
		output = new fl.controls.TextArea();
		output.tabChildren=false;
		output.mouseChildren=false;
		output.mouseEnabled=false;
		output.x = 10.after(users);
		output.y = users.y;
		output.width = 800;
		output.height = users.height;
		addChild(output);
		
		var label = new fl.controls.Label();
		label.text = userName;
		label.setStyle("textFormat",textFormat);
		label.width = users.width;
		label.x = users.x;
		label.y = 10.under(users);
		addChild(label);
		
		btn = new fl.controls.Button();
		btn.label="send";
		btn.x = output.x + output.width - btn.width;
		btn.y= 10.under(output);
		btn.addEventListener(flash.events.MouseEvent.CLICK, onSendBtn);
		addChild(btn);
			
		input = new fl.controls.TextInput();
		input.x = 10.after(label);
		input.y = 10.under(output);
		input.width = btn.x - 10 - input.x;
		input.height = btn.height;
		addChild(input);
	}
	public static function above(i:Int, t:Dynamic):Float
	{
		return cast t.y - t.height - i;
	}
	public static function under(i:Int, t:Dynamic):Float
	{
		return cast t.y + t.height + i;
	}
	public static function after(i:Int, t:Dynamic):Float
	{
		return cast t.x + t.width + i;
	}
	public static function before(i:Int, t:Dynamic):Float
	{
		return cast t.x - t.width - i;
	}
	function formatDate():String
	{
		var d:Date = Date.now();
		var h:Dynamic = d.getHours();
		if(h<10) h="0"+h;
		var m:Dynamic = d.getMinutes();
		if(m<10) m="0"+m;
		return "[" + h + ":" + m + "]";
	}
}
typedef User=
{
	var id:Int;
	var name:String;
}

